package com.twu.biblioteca.services;

import com.twu.biblioteca.Constants.Movies;
import com.twu.biblioteca.Constants.Users;
import com.twu.biblioteca.elements.Movie;
import com.twu.biblioteca.elements.User;
import com.twu.biblioteca.helper.ServiceHelper;

import java.io.IOException;
import java.util.List;

public class MoviePrintService {
    private MovieLogicService movieLogicService = new MovieLogicService(new Movies(), new Users());
    private User currentUser = null;

    public void showAllAvailableMovies() {
        System.out.println("\nAll available movies are:");
        List<Movie> allAvailableMovies = movieLogicService.getAllAvailableMovies();
        try {
            for (Movie movie : allAvailableMovies) {
                System.out.print(movie.getName() + "\t");
                System.out.print(movie.getDirector() + "\t");
                System.out.print(movie.getYear() + "\t");
                System.out.println(movie.getRating());
            }
        } catch (NullPointerException e) {
            System.out.println("no available movies for now!");
        }
    }

    public void login() throws IOException {
        System.out.println("\nPlease input your userId:");
        String userId = ServiceHelper.getInputValue();
        System.out.println("Please input your password");
        String password = ServiceHelper.getInputValue();

        boolean loginSuccess = movieLogicService.login(userId, password);

        if (loginSuccess) {
            currentUser = movieLogicService.getUserByUserId(userId);
        }
        else{
            System.out.println("login fail");
        }
    }

    public void checkout() throws IOException {
        if (currentUser == null) {
            System.out.println("Please login first!");
            return;
        }

        System.out.println("Please input the movie name for checkout:");
        String movieName = ServiceHelper.getInputValue();
        boolean checkoutSuccess = movieLogicService.checkout(movieName, currentUser);

        if (!checkoutSuccess) {
            System.out.println("This movie is not available");
            return;
        }

        System.out.println("Check out movie success!");
    }

    public void showUserInformationForCurrentUser() {
        if (currentUser == null) {
            System.out.println("Please login first before check user information!");
            return;
        }
        System.out.println("\nYour user information is shown below:");
        System.out.println("name: " + currentUser.getName());
        System.out.println("emailAddress: " + currentUser.getEmailAddress());
        System.out.println("phoneNumber: " + currentUser.getPhoneNumber());
    }
}
